package com.cade.agriculturemall.view.retrievePassword;

import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cade.agriculturemall.model.RegisterEntity;
import com.cade.agriculturemall.R;
import com.cade.agriculturemall.base.BaseActivity;
import com.cade.agriculturemall.util.ToastUtils;
import com.cade.agriculturemall.widget.CleanEditText;

public class RetrievePasswordActivity extends BaseActivity<RetrievePasswordContract.IPresenter> implements RetrievePasswordContract.IView, View.OnClickListener, View.OnFocusChangeListener {
    private Button mBtnNext;
    private TextView mTvPhone;
    private CleanEditText mEtPswFirst;
    private CleanEditText mEtPswTwice;
    private TextView mTvCode;
    private Button mBtnCode;
    private int type = 0;
    private RegisterEntity mEntity = new RegisterEntity();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrieve_password);
    }


    @Override
    public void onchangeBtnText() {

    }

    @Override
    public void setupView() {
        setupToolbar(true);
        mTvPhone = (TextView) findViewById(R.id.retrievepsw_et_phone);
        mTvCode = (TextView) findViewById(R.id.retrievepsw_et_code);
        mBtnNext = (Button) findViewById(R.id.retrievepsw_btn_register);
        mEtPswFirst = (CleanEditText) findViewById(R.id.retrievepsw_et_password_first);
        mEtPswTwice = (CleanEditText) findViewById(R.id.retrievepsw_et_password_twice);
        mEtPswFirst.setOnCleanFocusChangeListener(this);
        mEtPswTwice.setOnCleanFocusChangeListener(this);
        mBtnNext.setText("下一步");
        mBtnNext.setOnClickListener(this);

        mIvEyeFirst = (ImageView) findViewById(R.id.retrievepsw_imgview_eye_first);
        mIVEyeTwice = (ImageView) findViewById(R.id.retrievepsw_imgview_eye_twice);

        alterPswVision(mEtPswFirst, mIvEyeFirst);


        alterPswVision(mEtPswTwice, mIVEyeTwice);

    }

    private ImageView mIvEyeFirst;

    private int state = 0;
    private ImageView mIVEyeTwice;

    /**
     * 点击mIVEye设置密码可见/不可见
     */

    public void alterPswVision(final EditText editText, final ImageView imageView) {

        // mPresenter.checkSendVerifyButtonStatus();
        editText.setTransformationMethod(PasswordTransformationMethod.getInstance());//隐藏密码
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (state) {
                    case 0:
                        imageView.setImageResource(R.mipmap.see);
                        editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                        state++;
                        break;
                    case 1:
                        imageView.setImageResource(R.mipmap.no_see);
                        editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
                        state--;
                        break;
                }
            }
        });
    }

    @Override
    public void showOther(int type) {
        LinearLayout mLlPasswordFirst;
        LinearLayout mLlPasswordTwice;
        LinearLayout mLinearLayout;
        mLinearLayout = (LinearLayout) findViewById(R.id.retrievepsw_ll_code);
        mLlPasswordFirst = (LinearLayout) findViewById(R.id.retrievepsw_ll_password_first);
        mLlPasswordTwice = (LinearLayout) findViewById(R.id.retrievepsw_ll_password_twice);
        switch (type) {
            case 1:
                mLinearLayout.setVisibility(View.VISIBLE);
                mLlPasswordFirst.setVisibility(View.VISIBLE);
                mLlPasswordTwice.setVisibility(View.VISIBLE);
                mBtnNext.setText("修改密码");
                break;
        }
    }

    @Override
    public void setupEvent() {

    }

    @Override
    public void setupData() {

    }

    @Override
    public RetrievePasswordContract.IPresenter setupPresenter() {
        return new RetrievePasswordPresenter(this);
    }

    @Override
    public void onClick(View v) {
        if (type == 0) {
            mEntity.setPhone(mTvPhone.getText().toString());
            if (!TextUtils.isEmpty(mEntity.getPhone())) {
                mPresenter.getCode(mEntity.getPhone());
            } else {
                ToastUtils.showToast("请输入手机号码");
            }
        } else {

        }
    }


    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        String mFirstPsw = mEtPswFirst.getText().toString();
        String mTwicePsw = mEtPswTwice.getText().toString();

        if (!TextUtils.isEmpty(mTwicePsw) && !TextUtils.isEmpty(mFirstPsw)) {
            compare(mTwicePsw, mFirstPsw);
        }
    }

    public void compare(String s1, String s2) {
        if (s1.equals(s2)) {
        } else {
            ToastUtils.showToast("两次密码输入不同，请重新输入");
        }
    }
}
